/*
 * @lc app=leetcode.cn id=566 lang=cpp
 *
 * [566] 重塑矩阵
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<int>> matrixReshape(vector<vector<int>> &mat,
                                      int r, int c)
    {
        vector<vector<int>> ans;
        const int m = mat.size();
        const int n = mat[0].size();
        if (m * n != r * c)
            return mat;

        int k = 0;
        vector<int> tmp;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (k == c)
                {
                    k = 0;
                    ans.push_back(tmp);
                    tmp.clear();
                }

                tmp.push_back(mat[i][j]);
                ++k;
            }
        }
        if (tmp.size())
            ans.push_back(tmp);

        return ans;
    }
};

// int main(int argc, char const *argv[])
// {
//     vector<vector<int>> v = {{1, 2}, {3, 4}};
//     Solution().matrixReshape(v, 4, 1);
//     return 0;
// }

// @lc code=end
